#include <iostream>
#include <vector>
#include <string>
using namespace std;

vector<int> k[30]; 
int n;

typedef pair<int, int> PII;

PII find(int x) {
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < k[i].size(); j++) {
            if (k[i][j] == x) {
                return {i, j};
            }
        }
    }
    return {-1, -1}; 
}


void turnba(int x, int y) {
    for (int i = y + 1; i < k[x].size(); i++) {
        k[k[x][i]].push_back(k[x][i]);  
    }
    k[x].resize(y + 1);
}

int main() {
    cin >> n;
    for (int i = 0; i < n; i++) {
        k[i].push_back(i);
    }

    string str1, str2;
    int a, b;
    while (cin >> str1 >> a >> str2 >> b) {

        PII poa = find(a); 
        int xa = poa.first, ya = poa.second;

        PII pob = find(b);  
        int xb = pob.first, yb = pob.second;
        if (xa == xb) continue;  
    
        if (str1 == "move") {
            turnba(xa, ya);  
        }
        if (str2 == "onto") {
            turnba(xb, yb);
        }
        for (int i = ya; i < k[xa].size(); i++) {
            k[xb].push_back(k[xa][i]);
        }            
        k[xa].resize(ya);  
        }
       
        for (int i = 0; i < n; i++) {
            cout << i << ":";
            for (int j = 0; j < k[i].size(); j++) {
                cout  << " " << k[i][j];
            }
            cout << endl;
        }
    return 0;
}
